"""
Created on Sat Aug 24 16:39:31 2013

@author: chico
"""

from libatomism import *
from PySide import QtCore, QtGui
from msGeneralizedCoordinatesWidget import *
from simpletreemodel import *
from PopUpBox import *
import matplotlib.pyplot as plt

class msScalarFunctionWidget(msWidget):
    
    def __init__(self,data,mainwindows):
        super(msScalarFunctionWidget, self).__init__(data,mainwindows)
        
        """ First create the Units group box
        """
       
        #self.vbox.addWidget(QtGui.QLabel("Unit of returned value: "+ self.data.getUnit().getStr()))
        
        hbox = QtGui.QHBoxLayout() 
        vbox = QtGui.QVBoxLayout() 
        vbox.addStretch(1)
        self.resultLabel = QtGui.QLabel("Current field value: ")
        vbox.addWidget(self.resultLabel)

        hbox1=QtGui.QHBoxLayout() 
        self.buttonEvaluate = QtGui.QPushButton("recompute field")
        self.buttonEvaluate.clicked.connect(self.evaluateField)
        self.buttonEvaluate.setMinimumWidth(150)
        hbox1.addWidget(self.buttonEvaluate)
        hbox1.addStretch(1)
        hbox2=QtGui.QHBoxLayout() 
        self.buttonPlot = QtGui.QPushButton("plot 1D")
        self.buttonPlot.setMinimumWidth(150)
        self.buttonPlot.clicked.connect(self.plot1d)
        self.buttonPlot.setVisible(0)
        hbox2.addWidget(self.buttonPlot)
        hbox2.addStretch(1)
        hbox3=QtGui.QHBoxLayout() 
        self.buttonResetunit = QtGui.QPushButton("reset unit")
        self.buttonResetunit.setMinimumWidth(150)
        hbox3.addWidget(self.buttonResetunit)
        hbox3.addStretch(1)

        vbox.addLayout(hbox1)
        vbox.addLayout(hbox3)
        vbox.addLayout(hbox2)
        vbox.addStretch(1)
        hbox.addLayout(vbox)
        hbox.addSpacing(100)
        vbox = QtGui.QVBoxLayout() 
        self.constantsList = QtGui.QListView()  
        vbox.addWidget(self.constantsList)
        vbox.addWidget(QtGui.QPushButton("add constant"))
        hbox.addLayout(vbox)

        self.vbox.addLayout(hbox)
        
        self.vbox.addWidget(QtGui.QCheckBox("synchronization/coordinates"))
        self.gencoors = msGeneralizedCoordinatesWidget(self.data.getCoordinates() , self.mainwindows )
        self.gencoors.listView.clicked.connect(self.selectCoor)
        self.vbox.addWidget(self.gencoors )    
        self.gencoors.widgetScalVar.slider.valueChanged[int].connect(self.updateCoorChange)
        self.gencoors.widgetScalVar.valueSpinBox.valueChanged[float].connect(self.updateCoorChange)
        
        self.setMinimumHeight(self.gencoors.height()+250)
        self.setMaximumHeight(self.gencoors.height()+250)
        self.setMinimumWidth(500)
        self.setMaximumWidth(500)
        #self.evaluateField()

    def selectCoor(self):               
        self.buttonPlot.setVisible(1)
        self.indiceCoor = self.gencoors.listView.currentIndex().row()
        self.coor =self.gencoors.listView.currentIndex().internalPointer().getRawdata()

    def updateCoorChange(self):
        print "aaa"
        self.evaluateField()
 
    def evaluateField(self):
        result = 0
        try:
            result = self.data.evaluate()
        except:
            self.mainwindows.reportException(sys)
            
            
        self.resultLabel.setText("Current field value: " + str(result))
        return result
  
    def plot1d(self):
       
        plt.plot(self.data.getXSample1d(self.indiceCoor,200),self.data.getYSample1d(self.indiceCoor,200))
        plt.xlabel(self.coor.getId() + "[" + self.coor.getUnitStr() + "]" )
        plt.ylabel(self.data.getId() + " [" + self.data.getUnit().getStr() + "]")
        plt.show()